using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using AutoGear.BLL;
using ECommerce.Library;
using ECommerce.Library.Web;
using Entity;
using ECommerce.DAL;

public partial class receipt : SitePublicBasePage
{
    private ProductPurchaseOrder _order;
    public ProductPurchaseOrder Order
    {
        get
        {
            if (_order == null)
            {
                string orderNumber = string.Empty;
                //orderNumber = "32517";
                if (AutoGearSessionManager.OrderNumber != null)
                    orderNumber = AutoGearSessionManager.OrderNumber;
                else if (Request["oid"] != null && !String.IsNullOrEmpty(Request["oid"]))
                    orderNumber = Request["oid"];
                if (string.IsNullOrEmpty(SupplierId))
                {
                    _order = new ProductPurchaseOrder(orderNumber);
                }
                else
                {
                    _order = new ProductPurchaseOrder(orderNumber, SupplierId);
                }
                AutoGearSessionManager.OrderNumber = null;
            }
            return _order;
        }
    }

    public String SupplierId
    {
        get
        {
            try
            {

                return Page.RouteData.Values["partnerId"].ToString();
            }
            catch
            {
                return string.Empty;
            }
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            if (Order == null)
            {
                Response.Redirect(AutoGear.BLL.CustomSiteMap.OrderSummary);
            }

            this.Header.DataBind();

            ContentPlaceHolder ph = (ContentPlaceHolder)this.Master.Master.FindControl("cphMiddleContent");

            Literal litPageTitle = (Literal)ph.FindControl("litPageTitle");
            Literal litCurrentPage = (Literal)ph.FindControl("litCurrentPage");

            if (litPageTitle != null)
            {
                litCurrentPage.Text = litPageTitle.Text = "Receipt";

            }

            CustomCollection<TblGiftCertificateOrderItems> Giftitems = TblGiftCertificateOrderItems.GetTblGiftCertificateOrderItemsByOrderId(_order.OrderId);

            if (Order.ShoppingCart == null || Order.ShoppingCart.CartItems.Count <= 0)
            {
                if (Giftitems == null && Giftitems.Count <= 0)
                {
                    Response.Redirect(AutoGear.BLL.CustomSiteMap.ShoppingCart);
                }
            }
            BindData();
        }
    }

    private void BindData()
    {
        BindProductList();

        CreditCard card = (CreditCard)Order.CreditCard;
        ShoppingCart cart = Order.ShoppingCart;
        AutoGear.BLL.Address billingAddress = (AutoGear.BLL.Address)Order.BillingAddress;
        AutoGear.BLL.Address shippingAddress = (AutoGear.BLL.Address)Order.ShippingAddress;

        lblDate.Text = Order.OrderDate.ToShortDateString();
        lblRequestDeleveryDate.Text = Order.RequestedDeliveryDate.ToShortDateString();
        string Shippingmethod = string.Empty;
        if (Order.ProductShipping.ProductShippingMethod == "1")
        {
            Shippingmethod = "Ground";
        }
        else if (Order.ProductShipping.ProductShippingMethod == "2")
        {
            Shippingmethod = "3 Day Select";
        }
        else if (Order.ProductShipping.ProductShippingMethod == "3")
        {
            Shippingmethod = "2nd Day Air";
        }
        else if (Order.ProductShipping.ProductShippingMethod == "4")
        {
            Shippingmethod = "Next Day Air";
        }
        lblShipmentStatus.Text = Shippingmethod;
        lblOrderNo.Text = Order.OrderNumber;
        lblSubTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(cart.SubTotal + cart.GiftSubTotal);
        lblCouponDiscount.Text = "-" + ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.CouponDiscount);
        lblShippingHandling.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.ProductShipping.ProductShippingCost);
        lblTax.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.TaxAmount);
        lblTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.TotalAmount);

        lblBillingFirstName.Text = billingAddress.FirstName;
        lblBillingLastName.Text = billingAddress.LastName;
        lblBillingEmailAddress.Text = billingAddress.EmailAddress;
        lblBillingAddress.Text = billingAddress.AddressLine1 + "\n" + billingAddress.AddressLine2;
        lblBillingPhone.Text = billingAddress.TelephoneNumber;
        lblBillingPhone2.Text = billingAddress.TelephoneNumber2;
        lblBillingState.Text = billingAddress.AddressState;
        lblBillingZipCode.Text = billingAddress.ZipCode;

        lblShippingFirstName.Text = shippingAddress.FirstName;
        lblShippingLastName.Text = shippingAddress.LastName;
        lblShippingAddress.Text = shippingAddress.AddressLine1 + "\n" + shippingAddress.AddressLine2;
        lblShippingEmailAddress.Text = shippingAddress.EmailAddress;
        lblShippingPhone.Text = shippingAddress.TelephoneNumber;
        lblShippingCity.Text = shippingAddress.City;
        lblShippingState.Text = shippingAddress.AddressState;
        lblShippingZipCode.Text = shippingAddress.ZipCode;
        lblShippingState.Text = shippingAddress.AddressState;
         string orderStatus = (Order.OrderStatus.ToString() == OrderStatus.CCSuccess.ToString()) ? "Success" : "Failed";
        if (card != null)
        {
            if (Order.CreditCardAmount > 0)
            {
                if(orderStatus != "Failed")
                    ltlCreditCard.Text = "<div class='notice'>Total amount " + String.Format("{0:C}", Order.OrderAmount) + " have been deducted.</div>";
                else
                    ltlCreditCard.Text = "<div class='notice'>Total amount " + String.Format("{0:C}", Order.CreditCardAmount) + " have been deducted.</div>";
            }
        }
       
        //orderStatus = "Failed";
        if (orderStatus == "Failed")
            ltlMessage.Text = "<div class='error'>" + "We're sorry. There was a problem processing your order. <br/>We will contact you as soon as possible to resolve the issue or<br/> you may contact us. We appreciate your patience. </div>";
        else
            ltlMessage.Text = string.Empty;
    }

    private void BindProductList()
    {

        ShoppingCart cart = Order.ShoppingCart;
        rptProduct.DataSource = cart.CartItems;
        rptProduct.DataBind();

        //CustomCollection<TblGiftCertificateOrderItems> gftItems = TblGiftCertificateOrderItems.GetTblGiftCertificateOrderItemsByOrderId(_order.OrderId);
        //if (gftItems != null)
        //{
        //    int temp_code = gftItems[0].GiftCertificateId;
        //    TblGiftCertificate CurrentGiftCertificate = TblGiftCertificate.GetTblGiftCertificateByGiftCertificateId(temp_code);

        //    GiftItem giftItem = new GiftItem(CurrentGiftCertificate.GiftCertificateId.ToString(), CurrentGiftCertificate.Code, CurrentGiftCertificate.FromEmailAddress, CurrentGiftCertificate.FromName,
        //        CurrentGiftCertificate.ToEmailAddress, CurrentGiftCertificate.ToName, CurrentGiftCertificate.Message, CurrentGiftCertificate.Quantity, CurrentGiftCertificate.PriceAmount, CurrentGiftCertificate.DateCreated, CurrentGiftCertificate.DateExpired);

        //    cart.AddGiftItem(giftItem);

        //    rptGiftCertificate.DataSource = cart.GiftItems;
        //    rptGiftCertificate.DataBind();
        //}

        //if (rptProduct.Items.Count == 0)
        //{
        //    rptProduct.Visible = false;
        //}
        rptGiftCertificate.Visible = false;
        if (Order.Substitute.Length > 5) 
        {
            lblSubstitute.Text = Order.Substitute;
            lblSubstitute.Visible = true;
        }
        //imgGIftCard.ImageUrl = Order.GiftCardImageUrl;
        if (!String.IsNullOrEmpty(Order.SpecialInstruction))
        {
            lblComments.Visible = true;
            lblComments.Text = "<b>Comments</b> :  " + Order.SpecialInstruction;
            lblComments.Style.Add("height", "250px");
        }
        if (!String.IsNullOrEmpty(Order.GIftCardMessage))
        {
            lblGiftCardMsg.Visible = true;
            lblGiftCardMsg.Text = "<b>Gift Message</b> :  " + Order.GIftCardMessage;
            lblGiftCardMsg.Style.Add("height", "250px");
        }
        if (!String.IsNullOrEmpty(Order.OfficeUse))
        {
            lblOfficeUse.Visible = true;
            lblOfficeUse.Text = "<b>Office Use</b> :  " + Order.OfficeUse;
            lblOfficeUse.Style.Add("height", "250px");
        }

    }

    protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            CartItem cartItem = (CartItem)e.Item.DataItem;
            if (cartItem != null)
            {
                HyperLink hlkItem = (HyperLink)e.Item.FindControl("hlkItem");
                hlkItem.NavigateUrl = cartItem.Product.ProductUrl;
                if (cartItem.Product.ProductName != null)
                {
                    hlkItem.Text = cartItem.Product.ProductName;
                    hlkItem.Style.Add("text-decoration", "none");
                }
                else
                {
                    hlkItem.Text = "Name Deleted";
                    hlkItem.Style.Add("text-decoration", "none");
                }

                Label lblItemNumber = (Label)e.Item.FindControl("lblItemNumber");
                if (cartItem.Product.ItemNumber != null)
                    lblItemNumber.Text = cartItem.Product.ItemNumber;
                else
                    lblItemNumber.Text = "Deleted";

                Label lblItemPrice = (Label)e.Item.FindControl("lblItemPrice");
                lblItemPrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(cartItem.UnitPrice);

                Label lblQuantity = (Label)e.Item.FindControl("lblQuantity");
                lblQuantity.Text = cartItem.Quantity.ToString();

                Label lblItemTotal = (Label)e.Item.FindControl("lblItemTotal");
                lblItemTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(cartItem.ItemTotal);
            }

        }
        else if (e.Item.ItemType == ListItemType.Footer)
        {
        }
    }
    protected void imbPrintInvoice_Click(object sender, EventArgs e)
    {
        PrintInvoice();
    }

    protected void PrintInvoice()
    {
        string script = "<script type=\"text/javascript\" language=\"javascript\">window.print();</script>";
        ClientScript.RegisterClientScriptBlock(this.GetType(), "printscript", script);
    }
    protected void rptGiftCertificate_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            GiftItem giftItem = (GiftItem)e.Item.DataItem;

            Label lblGiftName = (Label)e.Item.FindControl("lblGiftName");
            lblGiftName.Text = "Name: " + giftItem.ToName + "  " + "Email: " + giftItem.ToEmailAddress;

            Label lblGiftItemPrice = (Label)e.Item.FindControl("lblGiftItemPrice");
            lblGiftItemPrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(giftItem.UnitPrice);

            Label lblGiftQuantity = (Label)e.Item.FindControl("lblGiftQuantity");
            lblGiftQuantity.Text = giftItem.Quantity.ToString();

            Label lblGiftItemTotal = (Label)e.Item.FindControl("lblGiftItemTotal");
            lblGiftItemTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(giftItem.ItemTotal);

        }
        else if (e.Item.ItemType == ListItemType.Footer)
        {

        }

    }   
}
